package com.supor.aiot.mine.ui.setting;


import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.supor.aiot.common.base.BaseMVVMActivity;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.bean.BossTAppParams;
import com.supor.aiot.mine.bean.FunctionIntroductionItem;
import com.supor.aiot.mine.databinding.ActivityFunctionIntroductionBinding;
import com.supor.aiot.mine.ui.adapter.FunctionIntroductionAdapter;

import java.util.ArrayList;
import java.util.List;

public class FunctionIntroductionActivity extends BaseMVVMActivity<FunctionIntroductionViewModel, ActivityFunctionIntroductionBinding> {

    private FunctionIntroductionAdapter introductionAdapter;
    private List<FunctionIntroductionItem> introductionItemList = new ArrayList<>();

    @Override
    protected int resourceId() {
        return R.layout.activity_function_introduction;
    }

    @Override
    protected void initView() {
        super.initView();
        bindingView.setViewM(this);
        bindingView.toolbar.setIvLeftOnClickListener(v -> finish());
        bindingView.toolbar.setTvLeftOnClickListener(v -> finish());
        initRecyclerView();
    }

    private void initRecyclerView() {
        bindingView.refreshLayout.setEnableLoadMore(false);
        bindingView.refreshLayout.setOnRefreshListener(refreshLayout -> {
            viewModel.getAppReleaseAll();
        });
        bindingView.refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            BossTAppParams.page++;
            viewModel.getAppReleaseAll();
        });

        bindingView.rcvAppsAll.setRefreshEnabled(false);
        bindingView.rcvAppsAll.setLoadMoreEnabled(false);
        bindingView.rcvAppsAll.setLayoutManager(new LinearLayoutManager(this));

        introductionAdapter = new FunctionIntroductionAdapter(introductionItemList);
        bindingView.rcvAppsAll.setAdapter(introductionAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        BossTAppParams.page = 1;
        viewModel.getAppReleaseAll();

        viewModel.getUpdateResult().observe(this, result -> {
            if (result instanceof Result.Success) {
                List<FunctionIntroductionItem> introductionItems = (List<FunctionIntroductionItem>) result.getData();
                if (introductionItems.size() == 0) {
                    bindingView.refreshLayout.setEnableLoadMore(false);
                    bindingView.tvNoneResult.setVisibility(View.VISIBLE);
                    introductionItemList.clear();
                } else {
                    bindingView.refreshLayout.setEnableLoadMore(true);
                    bindingView.tvNoneResult.setVisibility(View.GONE);
                    if (BossTAppParams.page == 1) {
                        introductionItemList.clear();
                    }
                    if (introductionItemList.size() < BossTAppParams.total) {
                        introductionItemList.addAll(introductionItems);
                    }
                }
                introductionAdapter.setFunctionIntroductionItems(introductionItemList);
                introductionAdapter.setNewData(introductionItemList);
                bindingView.refreshLayout.finishRefresh();
                bindingView.refreshLayout.finishLoadMore();
            }
        });
    }
}